#!/bin/sh
# vim: expandtab sw=4 ts=4 sts=4:

# Check for proper number of command line args.
if [ $# -ne 1 ]; then
  echo "Usage: `basename $0` {path_to_pma_root_folder}"
  exit 65
fi

# Check if we have ImageMagick
hash identify 2>&- ||  {
  echo "ERROR: ImageMagick not found on the system!"
  echo "Quitting..."
  exit 1
}

# Compress image, if possible
HAVE_PNGCRUSH=1
hash pngcrush 2>&- || {
  HAVE_PNGCRUSH=0
  echo "WARNING: 'pngcrush' not found, will not be able to compress the sprites"
}

# Icons that should not be included in the sprite
BLACKLIST="vertical_line.png spacer.png"

# Output filename for the sprite image
OUTPUT="sprites.png"

# Library file that will contain the information about
# individual images that are part of the sprite
LIBRARY="../sprites.lib.php"

if [ -d $1/themes ]; then
  cd $1/themes

  # For each theme
  for d in $(ls -d */); do
    # Go to folder that contains the images
    cd "$d"img
    echo "Processing folder: $PWD"
    FILES=''
    for f in $(ls *.png| LC_ALL=C sort); do
      VALID=true
      # Do not include blacklisted icons
      for b in $BLACKLIST; do
        if [ "$b" = "$f" ]; then
          VALID=false
        fi
      done
      if [ $VALID = false ]; then
        continue
      fi
      DATA=$(identify -ping $f || echo "NULL")
      if [ "$DATA" != "NULL" ]; then
        SIZE=$(echo $DATA | cut -d ' '  -f 3 | sed 's/x/ /')
        # Do not include icons that are larger than 16x16
        for s in $SIZE; do
          if [ $s -gt 16 ]; then
            VALID=false
          fi
        done
        if [ $VALID = true ]; then
          # Build the list of valid icons
          FILES="$FILES $f"
        fi
      fi
    done

    # Create an empty sprite of the correct size
    NUM_FILES=''
    for f in $FILES; do
      NUM_FILES=$(($NUM_FILES+1))
    done
    convert -size 16x$(($NUM_FILES*16+16)) xc:none temp.png

    # Add each icon to the sprite
    CURRENT=1
    for f in $FILES; do
      convert temp.png $f -geometry +0+$(($CURRENT*16)) -composite temp.png
      CURRENT=$(($CURRENT+1))
    done

    # Compress image, if possible
    if [ $HAVE_PNGCRUSH -eq 1 ]; then
      echo "Compressing file: $PWD/$OUTPUT"
      pngcrush -brute temp.png $OUTPUT > /dev/null
      rm -f temp.png
    else
      mv temp.png $OUTPUT
    fi

    # Generate the library file that contains the information
    # about individual images that are part of the sprite
    echo '<?php' > $LIBRARY
    echo '/**' >> $LIBRARY
    echo ' * AUTOGENERATED CONTENT - DO NOT EDIT!' >> $LIBRARY
    echo ' * ALL CHANGES WILL BE UNDONE!' >> $LIBRARY
    echo ' * RUN `./scripts/generate-sprites` TO UPDATE THIS FILE' >> $LIBRARY
    echo ' *'  >> $LIBRARY
    echo ' * @package PhpMyAdmin-theme' >> $LIBRARY
    echo ' */'  >> $LIBRARY
    echo '' >> $LIBRARY
    echo '/**' >> $LIBRARY
    echo ' * Returns map of sprites inside sprite.png'  >> $LIBRARY
    echo ' *'  >> $LIBRARY
    echo ' * @return array Data of sprites'  >> $LIBRARY
    echo ' */'  >> $LIBRARY
    echo "function PMA_sprites()" >> $LIBRARY
    echo "{" >> $LIBRARY
    echo "    return array(" >> $LIBRARY
    CURRENT=1
    for f in $FILES; do
      # Add a CSS rule for each icon in the sprite
      NAME=$(echo "'$f'" | sed 's/\.png//')

      DATA=$(identify -ping $f || echo "NULL")
      if [ "$DATA" != "NULL" ]; then
        SIZE=$(echo $DATA | cut -d ' '  -f 3 | sed 's/x/ /')
        WIDTH=0
        HEIGHT=0
        for s in $SIZE; do
          if [ $WIDTH = 0 ]; then
            WIDTH=$s
          else
            HEIGHT=$s
          fi
        done
      fi
      echo "        $NAME => array("               >> $LIBRARY
      echo "            'position' => '$CURRENT'," >> $LIBRARY
      echo "            'width'    => '$WIDTH',"   >> $LIBRARY
      echo "            'height'   => '$HEIGHT'"   >> $LIBRARY
      echo "        ),"                            >> $LIBRARY
      CURRENT=$(($CURRENT+1))
    done
    echo "    );" >> $LIBRARY
    echo "}" >> $LIBRARY
    echo "?>" >> $LIBRARY

    # Back to the parent folder
    cd ../..
  done
  exit 0
else
  echo "ERROR: could not find the 'themes' folder in '`readlink -f $1`'"
  exit 1
fi
